home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Frameworks / Extension Shell 1.3 / Sample Extensions / Generic ES Handler ƒ / ES Handler.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-06  |  11.2 KB  |  357 lines  |  [TEXT/R*ch]

  1. /*    NAME:
  2.         ES Handler.c
  3.  
  4.     WRITTEN BY:
  5.         Dair Grant
  6.                 
  7.     DESCRIPTION:
  8.         This file contains a CODE resource to be used as a handler by
  9.         Extension Shell.
  10.  
  11.     NOTES:
  12.         •    Provides a basic shell for creating ES Handler CODE resources.
  13.             You will have to fill in the more Extension specific details,
  14.             and alter the #defines in the .h file to suit.
  15.  
  16.     ___________________________________________________________________________
  17.  
  18.     VERSION HISTORY:
  19.         (Jan 1994, dg)
  20.             •    First publicly distributed version.
  21.  
  22.  
  23.     ___________________________________________________________________________
  24. */
  25. //=============================================================================
  26. //        Include files 
  27. //-----------------------------------------------------------------------------
  28. #include "ParamBlock.h"
  29. #include "StandaloneCode.h"
  30. #include "ESConstants.h"
  31. #include "ES Handler.h"
  32. #include "CodeConstants.h"
  33.  
  34.  
  35.  
  36.  
  37.  
  38. //=============================================================================
  39. //        Private function prototypes
  40. //-----------------------------------------------------------------------------
  41. void    main(short theMsg, ESParamBlock *theParamBlock);
  42. void    InitialiseParamBlock(void);
  43. void    InitialiseAddrsTable(void);
  44. void    HandleTheError(void);
  45. void    SetUpIcons(int animDelay, int numIcons, int firstIcon);
  46.  
  47.  
  48.  
  49.  
  50.  
  51. //=============================================================================
  52. //        Global variables
  53. //-----------------------------------------------------------------------------
  54. ESParamBlock    *gTheParamBlock;
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. //=============================================================================
  66. //        main : Entry point to our code resource.
  67. //-----------------------------------------------------------------------------
  68. //        Note :    Extension Shell communicates with us via a message constant,
  69. //                and a pointer to a structure it owns. Our job is to fill in
  70. //                the details, depending on what it wants us to do. It takes
  71. //                care of all actual work.
  72. //-----------------------------------------------------------------------------
  73. void main(short theMsg, ESParamBlock *theParamBlock)
  74. {
  75.  
  76.  
  77.  
  78.  
  79.     // Set up A4 so that we can access our globals, and initialise them.
  80.     GetGlobals();
  81.     gTheParamBlock = theParamBlock;
  82.  
  83.  
  84.  
  85.     // Case out on what we have to do
  86.     switch(theMsg) {
  87.         case kInitialiseParamBlock:
  88.              InitialiseParamBlock();
  89.              break;
  90.              
  91.         case kInitialiseAddrsTable:
  92.              InitialiseAddrsTable();
  93.              break;
  94.  
  95.         case kHandleError:
  96.              HandleTheError();
  97.              break;
  98.     
  99.         default:
  100.              ;
  101.     }
  102.  
  103.  
  104.  
  105.     // Restore A4.
  106.     UngetGlobals();
  107. }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. //=============================================================================
  119. //        InitialiseParamBlock : Initialises the ParamBlock.                                                                 
  120. //-----------------------------------------------------------------------------
  121. //        Note :    We have three things we want to do:
  122. //                    • Check to see if we can still run
  123. //                    • Set up the icons we want to display
  124. //                    • Set up the code we want installed
  125. //-----------------------------------------------------------------------------
  126. void InitialiseParamBlock(void)
  127. {
  128.  
  129.  
  130.  
  131.  
  132.     // Check for System 7. We depend on having System 7, and won't
  133.     // run if we don't have it. We beep, post an error message,
  134.     // and show our disabled icon(s).
  135.     if (gTheParamBlock->systemVersion < 0x0700)
  136.         {
  137.         // Error details
  138.         gTheParamBlock->beepNow                = true;
  139.         gTheParamBlock->postError            = true;
  140.         gTheParamBlock->errorStringsID        = kErrorStrings;
  141.         gTheParamBlock->errorStringIndex    = kNeedSystemSeven;
  142.  
  143.  
  144.         // Icon details
  145.         SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
  146.         }
  147.     
  148.     
  149.     
  150.     // If a shift key, or the mouse button, is down, we don't load either.
  151.     // We don't post an error, but we do show our disabled icon(s).
  152.     else if ((*gTheParamBlock->UserForcedDisable)(kShiftKey, true))
  153.         {
  154.         // Icon details
  155.         SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
  156.         }
  157.     
  158.     
  159.     
  160.     // Otherwise, we're allowed to run so we show our icon(s) as normal,
  161.     // and fill in the details for the code we want installed.
  162.     else
  163.         {
  164.         // Icon details
  165.         SetUpIcons(kEnabledAnimDelay, kMyNumEnabledIcons, kMyFirstEnabledIcon);
  166.         
  167.         
  168.         // From this point on, the code to be installed is Extension specific, and is
  169.         // therefore shown as pseudo-code. Actual code will be very similar, and can
  170.         // be based on what is shown here. All you will have to do is cut out the
  171.         // irrelevent code, and supply expressions for the right hand side of the
  172.         // statements. Obviously, the indexes into theCodeResources will need to be
  173.         // changed, and you may need more of one given type of code. The indexes
  174.         // should correspond to the error handling code later on, by some #defines
  175.         // in a common .h file ('CodeConstants.h' by default). By placing them in
  176.         // a common .h file, your standalone code can use the #define as an index into
  177.         // the Extension Shell address table to find out what it replaced (if anything).
  178.         // This way, a given code resource will have the same index in the theCodeResources
  179.         // array, the errorIndex value, and the address table. If you need to use an
  180.         // address table, you will have to provide a value for the addressTableSelector
  181.         // field.
  182. /*
  183.         // General details
  184.         gTheParamBlock->installAddressTable        = // Install an address table?
  185.         gTheParamBlock->addressTableSelector    = // Selector of any address table.
  186.         gTheParamBlock->numCodeResources        = // Number of code resources to be installed.
  187.  
  188.  
  189.         // Details for a trap patch
  190.         gTheParamBlock->theCodeResources[kCodeThingOne].resType        = // Resource type for code
  191.         gTheParamBlock->theCodeResources[kCodeThingOne].resID        = // Resource ID for code
  192.         gTheParamBlock->theCodeResources[kCodeThingOne].codeType    = kTrapPatchType;
  193.         gTheParamBlock->theCodeResources[kCodeThingOne].theCodeThing.theTrapPatch.trapNum = // Trap number
  194.  
  195.  
  196.         // Details for a Gestalt selector
  197.         gTheParamBlock->theCodeResources[kCodeThingTwo].resType        = // Resource type for code
  198.         gTheParamBlock->theCodeResources[kCodeThingTwo].resID        = // Resource ID for code
  199.         gTheParamBlock->theCodeResources[kCodeThingTwo].codeType    = kGestaltSelectorType;
  200.         gTheParamBlock->theCodeResources[kCodeThingTwo].theCodeThing.theGestaltSelector.theSelector = // New selector
  201.         gTheParamBlock->theCodeResources[kCodeThingTwo].theCodeThing.theGestaltSelector.overwriteExistingSelector = // Replace any existing selector?
  202.  
  203.  
  204.         // Details for a shutdown task
  205.         gTheParamBlock->theCodeResources[kCodeThingThree].resType    = // Resource type for code
  206.         gTheParamBlock->theCodeResources[kCodeThingThree].resID        = // Resource ID for code
  207.         gTheParamBlock->theCodeResources[kCodeThingThree].codeType    = kShutdownTaskType;
  208.         gTheParamBlock->theCodeResources[kCodeThingThree].theCodeThing.theShutdownTask.theFlags = // Task flags
  209.  
  210.  
  211.         // Details for a VBL task
  212.         gTheParamBlock->theCodeResources[kCodeThingFour].resType    = // Resource type for code
  213.         gTheParamBlock->theCodeResources[kCodeThingFour].resID        = // Resource ID for code
  214.         gTheParamBlock->theCodeResources[kCodeThingFour].codeType    = kVBLTaskType;
  215.         gTheParamBlock->theCodeResources[kCodeThingFour].theCodeThing.theVBLTask.vblCount = // Count
  216.         gTheParamBlock->theCodeResources[kCodeThingFour].theCodeThing.theVBLTask.vblPhase = // Phase
  217.  
  218.  
  219.         // Details for a low-memory filter
  220.         gTheParamBlock->theCodeResources[kCodeThingFive].resType    = // Resource type for code
  221.         gTheParamBlock->theCodeResources[kCodeThingFive].resID        = // Resource ID for code
  222.         gTheParamBlock->theCodeResources[kCodeThingFive].codeType    = kLowMemFilterType;
  223.         gTheParamBlock->theCodeResources[kCodeThingFive].theCodeThing.theLowMemFilter.theEntryPoint = // Entry point for filter
  224.  
  225.  
  226.         // Details for a block of code
  227.         gTheParamBlock->theCodeResources[kCodeThingSix].resType        = // Resource type for code
  228.         gTheParamBlock->theCodeResources[kCodeThingSix].resID        = // Resource ID for code
  229.         gTheParamBlock->theCodeResources[kCodeThingSix].codeType    = kCodeBlockType;
  230.         gTheParamBlock->theCodeResources[kCodeThingSix].theCodeThing.theCodeBlock.reserved = // Dummy field
  231. */    
  232.         }
  233. }
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244. //=============================================================================
  245. //        InitialiseAddrsTable : Initialise the address table.                                                     
  246. //-----------------------------------------------------------------------------
  247. //        Note :    If we are being used in a Control Panel, we will probably have
  248. //                implemented the address table code with a custom code resource
  249. //                that returns a structure with an address table embedded at the
  250. //                start. This function's job is to correctly initialise the
  251. //                extended fields of that structure. If we're not using a
  252. //                (custom) address table then we don't do anything.
  253. //
  254. //                The message for this routine will only arrive if we've
  255. //                requested an address table.
  256. //-----------------------------------------------------------------------------
  257. void InitialiseAddrsTable(void)
  258. {
  259.  
  260.  
  261.  
  262. }
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273. //=============================================================================
  274. //        HandleTheError : Handle any errors.                                                         
  275. //-----------------------------------------------------------------------------
  276. //        Note :    If any error occurs, we beep, post an error, and remove our
  277. //                code. We also have to reset the icon details to show our
  278. //                disabled icons.
  279. //-----------------------------------------------------------------------------
  280. void HandleTheError(void)
  281. {
  282.  
  283.  
  284.  
  285.  
  286.     // General error handling settings
  287.     gTheParamBlock->removeInstalledCode    = true;
  288.     gTheParamBlock->beepNow                = true;
  289.     gTheParamBlock->postError            = true;
  290.     gTheParamBlock->errorStringsID        = kErrorStrings;
  291.  
  292.  
  293.  
  294.     // Icon details
  295.     SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
  296.  
  297.  
  298.  
  299.     // Case out on the error, and post a relevent message. Only a skeleton
  300.     // is shown here, and you should fill in the relevent details for the
  301.     // errors you support. You may also want to take account of the value
  302.     // of gTheParamBlock->theErr when deciding what string to post. The
  303.     // values of errorIndex will depend on what's being installed (from
  304.     // above). The #defines used to describe errorIndex will need to be
  305.     // tailored to your Extension's needs - they are given arbitrary names
  306.     // in this example. See CodeConstants.h for the definitions.
  307.     switch(gTheParamBlock->errorIndex) {
  308.         case kCodeThingOne:
  309.              gTheParamBlock->errorStringIndex = kErrorWithOne;
  310.              break;
  311.              
  312.         case kCodeThingTwo:
  313.              gTheParamBlock->errorStringIndex = kErrorWithTwo;
  314.              break;
  315.              
  316.         case kCodeThingThree:
  317.              gTheParamBlock->errorStringIndex = kErrorWithThree;
  318.              break;
  319.              
  320.         case kCodeThingFour:
  321.              gTheParamBlock->errorStringIndex = kErrorWithFour;
  322.              break;
  323.              
  324.         default:
  325.              gTheParamBlock->errorStringIndex = kUnknownError;
  326.     }
  327. }
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338. //=============================================================================
  339. //        SetUpIcons : Set up our icons accordingly.                                                         
  340. //-----------------------------------------------------------------------------
  341. //        Note :    We are passed in the resource ID of the first icon, the number
  342. //                of icons, and a delay for animation. We just fill in the fields
  343. //                in the paramBlock.
  344. //-----------------------------------------------------------------------------
  345. void SetUpIcons(int animDelay, int numIcons, int firstIcon)
  346. {    int        i;
  347.  
  348.  
  349.  
  350.  
  351.     // Fill in the fields
  352.     gTheParamBlock->animationDelay    = animDelay;
  353.     gTheParamBlock->numIcons        = numIcons;
  354.     for (i = 1; i <= numIcons; i++)
  355.         gTheParamBlock->theIcons[i] = firstIcon + i - 1;
  356. }
  357.